package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Organization;
import cn.wolfcode.crm.query.impl.OrganizationQuery;
import cn.wolfcode.crm.service.IOrganizationService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("organization")
public class OrganizationController {
    @Autowired
    IOrganizationService organizationService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"organization:view","机构页面"},logical= Logical.OR)
    public String view() {
        return "organization";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public List<Organization> getAll() {
        return organizationService.getAll();
    }
    @RequestMapping("changeState")
    @RequiresPermissions(value = {"organization:change","修改合作状态"},logical= Logical.OR)
    @ResponseBody
    public JSONResult changeState(Long id){
        try {
            organizationService.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"状态更新失败");
        }
        return new JSONResult(true,"状态更新成功");
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"organization:save","保存机构"},logical= Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Organization organization){
        try {
            organizationService.saveOrUpdate(organization);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(OrganizationQuery qo) {
        return organizationService.query(qo);
    }
}
